from rest_framework import status
from rest_framework.permissions import BasePermission, IsAdminUser

from users.models import User
from rest_framework.response import Response

class IsDoctor(BasePermission):
    def has_permission(self, request, view):
        return request.user and request.user.groups.filter(name='Врачи').exists()


class IsHR(BasePermission):
    def has_permission(self, request, view):
        return request.user and request.user.groups.filter(name='Отдел кадров').exists()


class IsChief(BasePermission):
    def has_permission(self, request, view):
        return request.user and request.user.groups.filter(name='Руководитель референс-центра').exists()


class IsSelfDoctor(BasePermission):
    def has_permission(self, request, view):
        if view.action == ['update', 'partial_update','create','destroy']:
            user_id = request.data.get('user')
            try:
                user = User.objects.get(pk=user_id)
            except User.DoesNotExist:
                return Response({'status': 'user not found'}, status=status.HTTP_404_NOT_FOUND)
            return user==request.user and request.user.groups.filter(name='Врач').exists()
        return request.user and request.user.groups.filter(name='Врач').exists()


class IsAdminOrHROrChief(BasePermission):
    def has_permission(self, request, view):
        is_admin = IsAdminUser().has_permission(request, view)
        is_hr = IsHR().has_permission(request, view)
        is_chief = IsChief().has_permission(request, view)
        return is_admin or is_hr or is_chief
